Repository navigation
Block Supports: Add a helper that returns every support's classes and styles - #15485
aaronrobertshaw wants to merge 15 commits into
Conversation
… is null or empty.
…nd spacing helpers.
… styles. Adds wp_get_block_supports_classes_and_styles(), which combines the background, color, border, shadow and spacing helpers in the style engine's order. An optional list limits it to the supports a block applies to one element.
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. Props Bot commandsYou can trigger Props Bot actions by commenting on this PR:
|
ramonjd
left a comment
There was a problem hiding this comment.
Backport is accurate. I smoke tested a bunch of blocks on the frontend, checked the CSS and classnames.
Tests pass locally.
Might be worth holding off until 7.2 is stable to avoid clashes?
Thanks!
westonruter
left a comment
There was a problem hiding this comment.
As with #14094 (review), I ran the changes to src through PHPStan level 8 and it reported back these opportunities for further refinement.
I added a suggestion to fix the issues in border.php.
The report:
------ ------------------------------------------------------------------------------------------------------------------------------------------------------------------
Line src/wp-includes/block-supports/background.php
------ ------------------------------------------------------------------------------------------------------------------------------------------------------------------
47 Function wp_get_background_classes_and_styles() has parameter $block_attributes with no value type specified in iterable type array.
💡 See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type
🪪 missingType.iterableValue
47 Function wp_get_background_classes_and_styles() return type has no value type specified in iterable type array.
💡 See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type
🪪 missingType.iterableValue
------ ------------------------------------------------------------------------------------------------------------------------------------------------------------------
------ ------------------------------------------------------------------------------------------------------------------------------------------------------------------
Line src/wp-includes/block-supports/border.php
------ ------------------------------------------------------------------------------------------------------------------------------------------------------------------
120 Function wp_get_border_classes_and_styles() has parameter $block_attributes with no value type specified in iterable type array.
💡 See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type
🪪 missingType.iterableValue
120 Function wp_get_border_classes_and_styles() return type has no value type specified in iterable type array.
💡 See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type
🪪 missingType.iterableValue
------ ------------------------------------------------------------------------------------------------------------------------------------------------------------------
------ ------------------------------------------------------------------------------------------------------------------------------------------------------------------
Line src/wp-includes/block-supports/colors.php
------ ------------------------------------------------------------------------------------------------------------------------------------------------------------------
161 Function wp_get_color_classes_and_styles() has parameter $block_attributes with no value type specified in iterable type array.
💡 See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type
🪪 missingType.iterableValue
161 Function wp_get_color_classes_and_styles() return type has no value type specified in iterable type array.
💡 See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type
🪪 missingType.iterableValue
------ ------------------------------------------------------------------------------------------------------------------------------------------------------------------
------ ------------------------------------------------------------------------------------------------------------------------------------------------------------------
Line src/wp-includes/block-supports/shadow.php
------ ------------------------------------------------------------------------------------------------------------------------------------------------------------------
76 Function wp_get_shadow_classes_and_styles() has parameter $block_attributes with no value type specified in iterable type array.
💡 See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type
🪪 missingType.iterableValue
76 Function wp_get_shadow_classes_and_styles() return type has no value type specified in iterable type array.
💡 See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type
🪪 missingType.iterableValue
------ ------------------------------------------------------------------------------------------------------------------------------------------------------------------
------ ------------------------------------------------------------------------------------------------------------------------------------------------------------------
Line src/wp-includes/block-supports/spacing.php
------ ------------------------------------------------------------------------------------------------------------------------------------------------------------------
82 Function wp_get_spacing_classes_and_styles() has parameter $block_attributes with no value type specified in iterable type array.
💡 See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type
🪪 missingType.iterableValue
82 Function wp_get_spacing_classes_and_styles() return type has no value type specified in iterable type array.
💡 See: https://phpstan.org/blog/solving-phpstan-no-value-type-specified-in-iterable-type
🪪 missingType.iterableValue
------ ------------------------------------------------------------------------------------------------------------------------------------------------------------------
[ERROR] Found 10 errors on changed lines (base: trunk @ 2f8310cf91)
| */ | ||
| function wp_get_border_classes_and_styles( $block_attributes ) { | ||
| if ( ! is_array( $block_attributes ) ) { | ||
| return array(); | ||
| } |
There was a problem hiding this comment.
| */ | |
| function wp_get_border_classes_and_styles( $block_attributes ) { | |
| if ( ! is_array( $block_attributes ) ) { | |
| return array(); | |
| } | |
| * | |
| * @phpstan-param array{ style?: array{ border?: array<string, mixed>, ...<string, mixed> }, ...<string, mixed> } $block_attributes | |
| * @phpstan-return array{ | |
| * class?: string, | |
| * style?: string, | |
| * } | |
| */ | |
| function wp_get_border_classes_and_styles( array $block_attributes ): array { |
| } | ||
|
|
||
| if ( ! $has_border_radius_support || $skip_radius ) { | ||
| unset( $block_attributes['style']['border']['radius'] ); | ||
| } | ||
|
|
||
| $border_block_styles['radius'] = $border_radius; | ||
| if ( ! $has_border_style_support || $skip_style ) { | ||
| unset( $block_attributes['style']['border']['style'] ); | ||
| } | ||
|
|
||
| // Border style. | ||
| if ( | ||
| wp_has_border_feature_support( $block_type, 'style' ) && | ||
| isset( $block_attributes['style']['border']['style'] ) && | ||
| ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'style' ) | ||
| ) { | ||
| $border_block_styles['style'] = $block_attributes['style']['border']['style']; | ||
| if ( ! $has_border_width_support || $skip_width ) { | ||
| unset( $block_attributes['style']['border']['width'] ); | ||
| } | ||
|
|
||
| // Border width. | ||
| if ( | ||
| $has_border_width_support && | ||
| isset( $block_attributes['style']['border']['width'] ) && | ||
| ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'width' ) | ||
| ) { | ||
| $border_width = $block_attributes['style']['border']['width']; | ||
| if ( ! $has_border_color_support || $skip_color ) { | ||
| unset( $block_attributes['style']['border']['color'], $block_attributes['borderColor'] ); | ||
| } |
There was a problem hiding this comment.
These unset() statements can be moved to an else:
| } | |
| if ( ! $has_border_radius_support || $skip_radius ) { | |
| unset( $block_attributes['style']['border']['radius'] ); | |
| } | |
| $border_block_styles['radius'] = $border_radius; | |
| if ( ! $has_border_style_support || $skip_style ) { | |
| unset( $block_attributes['style']['border']['style'] ); | |
| } | |
| // Border style. | |
| if ( | |
| wp_has_border_feature_support( $block_type, 'style' ) && | |
| isset( $block_attributes['style']['border']['style'] ) && | |
| ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'style' ) | |
| ) { | |
| $border_block_styles['style'] = $block_attributes['style']['border']['style']; | |
| if ( ! $has_border_width_support || $skip_width ) { | |
| unset( $block_attributes['style']['border']['width'] ); | |
| } | |
| // Border width. | |
| if ( | |
| $has_border_width_support && | |
| isset( $block_attributes['style']['border']['width'] ) && | |
| ! wp_should_skip_block_supports_serialization( $block_type, '__experimentalBorder', 'width' ) | |
| ) { | |
| $border_width = $block_attributes['style']['border']['width']; | |
| if ( ! $has_border_color_support || $skip_color ) { | |
| unset( $block_attributes['style']['border']['color'], $block_attributes['borderColor'] ); | |
| } | |
| } else { | |
| if ( ! $has_border_radius_support || $skip_radius ) { | |
| unset( $block_attributes['style']['border']['radius'] ); | |
| } | |
| if ( ! $has_border_style_support || $skip_style ) { | |
| unset( $block_attributes['style']['border']['style'] ); | |
| } | |
| if ( ! $has_border_width_support || $skip_width ) { | |
| unset( $block_attributes['style']['border']['width'] ); | |
| } | |
| if ( ! $has_border_color_support || $skip_color ) { | |
| unset( $block_attributes['style']['border']['color'], $block_attributes['borderColor'] ); | |
| } | |
| } |
There was a problem hiding this comment.
Oops. My suggestion included too many deletion lines. Anyway, I think you see what I was trying to show.
| * | ||
| * @param WP_Block_Type $block_type Block type. | ||
| * @param array $block_attributes Block attributes. | ||
| * @return array Border CSS classes and inline styles. |
There was a problem hiding this comment.
| * @return array Border CSS classes and inline styles. | |
| * | |
| * @phpstan-param array{ style?: array{ border?: array<string, mixed>, ...<string, mixed> }, ...<string, mixed> } $block_attributes |
Backport of Gutenberg PR WordPress/gutenberg#86337
Trac ticket: https://core.trac.wordpress.org/ticket/66485
Adds
wp_get_block_supports_classes_and_styles(), which returns the combined classes and styles of the background, color, border, shadow and spacing helpers. An optional list limits it to the supports a block applies to one element.This PR backports:
wp_get_block_supports_classes_and_styles(), next to the other block support utilities,The Avatar and Post Navigation Link changes that call it arrive later with the Gutenberg package update.
Dependency: stacked on #14094, which adds the five helpers this one calls. Once #14094 is committed, this branch should be rebased onto trunk.
Testing instructions
Nothing renders differently, because no block calls the helper in Core yet. To check its output:
It outputs only the border and shadow, without the padding:
Without this PR the command fails with a fatal error for an undefined function.
Use of AI
AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5.5
Used for: the port from Gutenberg and the tests; I reviewed and edited them.